package com.dlc.felear.xiaoensale.customView;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.utils.InputMethodManagerUtil;


public class MultInputDialog extends Dialog implements View.OnClickListener, View.OnFocusChangeListener {
    private static final String TAG="MultInputDialog";
    private Button btnCancel, btnConfirm;
    private EditText editText1,editText2,editText3,editText4;
    private TextView tvTips1, tvTips2, tvTips3, tvTips4;
    private EditText tempEditText;
    private Context context;
    private AppCompatActivity activity;
    private MultiDialogCallback callback;
    private String id;

    public MultInputDialog(@NonNull Context context, MultiDialogCallback callback) {
        super(context);
        this.callback = callback;
        initView(context);
    }

    public void setEtInputType(int type1, int type2, int type3,int type4) {
        editText1.setInputType(type1);
        editText2.setInputType(type2);
        editText3.setInputType(type3);
        editText4.setInputType(type4);
    }

    public MultInputDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        this.activity = (AppCompatActivity) context;
        View root = LayoutInflater.from(context).inflate(R.layout.mult_input_dialog, null);
        btnCancel = root.findViewById(R.id.btn_cancel);
        btnConfirm = root.findViewById(R.id.btn_confirm);
        tvTips1 = root.findViewById(R.id.tv_tips1);
        tvTips2 = root.findViewById(R.id.tv_tips2);
        tvTips3 = root.findViewById(R.id.tv_tips3);
        tvTips4 = root.findViewById(R.id.tv_tips4);
        editText1 = root.findViewById(R.id.et_value_1);
        editText2 = root.findViewById(R.id.et_value_2);
        editText3 = root.findViewById(R.id.et_value_3);
        editText4 = root.findViewById(R.id.et_value_4);
        tempEditText = editText1;
        btnConfirm.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        setContentView(root);
        setCanceledOnTouchOutside(true);
        setListence();
    }

    private void setListence() {
        editText1.setOnFocusChangeListener(this);
        editText2.setOnFocusChangeListener(this);
        editText3.setOnFocusChangeListener(this);
    }


    public void showDialog(String id) {
        Log.e(TAG, "id=" + id);
        tempEditText = editText1;
        InputMethodManagerUtil.inputMethodStatus(activity, tempEditText, false);
        this.id = id;
        show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                callback.multDialogInputResult(getValue(editText1), getValue(editText2),
                        getValue(editText3),getValue(editText4), id);
                clearData();
                dismiss();
                break;
            case R.id.btn_cancel:
                clearData();
                dismiss();
                break;
            default:
        }
    }


    public void setTips(String tips1, String tips2, String tips3, String tips4) {
        tvTips1.setText(tips1);
        tvTips2.setText(tips2);
        tvTips3.setText(tips3);
        tvTips4.setText(tips4);
    }

    private void clearData() {
        InputMethodManagerUtil.inputMethodStatus(activity, tempEditText, false);
        editText1.setText("");
        editText2.setText("");
        editText3.setText("");
        editText4.setText("");
        editText1.setHint("");
        editText2.setHint("");
        editText3.setHint("");
        editText4.setHint("");
    }

    public void setHintMsg(String ms1, String ms2, String ms3, String ms4) {
        editText1.setHint(ms1);
        editText2.setHint(ms2);
        editText3.setHint(ms3);
        editText4.setHint(ms4);
    }

    private String getValue(EditText et) {
        String temp = "0";
        String ss = et.getText().toString().trim();
        if (!TextUtils.isEmpty(ss)) {
            temp = ss;
        }else {
            temp=et.getHint().toString();
        }
        return temp;
    }

    private void showToast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            tempEditText = (EditText) v;
        }
    }

    public interface MultiDialogCallback {
        void multDialogInputResult(String value1, String value2, String value3, String value4, String id);
    }
}
